# -*- coding: utf-8 -*-
import logging
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
_logger = logging.getLogger(__name__)
import datetime

class RollCategory(models.Model):
    # 类型
    _name = 'roll.category'

    name = fields.Char('名称', required=True, index=True)
    power = fields.Integer('权重',default=50)

class RollItem(models.Model):
    # 待roll事项
    _name = 'roll.item'

    name = fields.Char('名称', required=True, index=True)
    category_id = fields.Many2one('roll.category', string=u'类别')
    progress = fields.Selection([
        ('p_10','待获取'),
        ('p_20','待执行'),
        ('p_30','执行中'),
        ('p_40','已完成'),
        ('p_50','已失败')
    ],string=u'进度',default='p_20')

# 顺手做的每月补贴记账系统
class RollChild(models.Model):
    # 孩子
    _name = 'roll.child'

    name = fields.Char('姓名', required=True)
    money_month = fields.Float('每月存入',digits=(32,2),default=0)
    now_money = fields.Float('当前余额',digits=(32,2),default=0)
    cast_ids = fields.One2many('roll.cast','child_id',string=u'流水记录')

class RollCast(models.Model):
    # 流水记账
    _name = 'roll.cast'
    name = fields.Char('名称')
    child_id = fields.Many2one('roll.child',string=u'姓名',required=True)
    category = fields.Selection([
        ('in','入账'),
        ('out','消费'),
    ],string=u'类型',default='out',required=True)
    money = fields.Float('金额',digits=(32,2),default=0)
    remark = fields.Char('备注')

    @api.model_create_multi
    def create(self, vals):
        cast = super(RollCast, self).create(vals)
        # 创建时更新余额
        if cast.category == 'in':
            cast.child_id.now_money = cast.child_id.now_money + cast.money
        if cast.category == 'out':
            cast.child_id.now_money = cast.child_id.now_money - cast.money
        return cast